﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddGlobalStringMapPage.aspx.cs" Inherits="Easy.Web.Pages.EntityPages.AddGlobalStringMapPage" %>

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="/_css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="/_css/common.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="/_css/iconfont.css" />
    <script src="/_js/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="/_js/bootstrap.min.js" type="text/javascript"></script>
    <script src="/_js/json2.js" type="text/javascript"></script>
    <script  type="text/javascript">
        function addGlobalStringMap(type) {

            var picklistvalue = "";
            $("#picklist_table tr[id]").each(function () {
                var picklistid = $(this).attr("picklistid");
                var value = $(this).find("td").eq(0).children().val();
                var key = $(this).find("td").eq(1).children().val();
                var order = $(this).find("td").eq(2).children().val();
                picklistvalue += picklistid + "α" + value + "α" + key + "α" + order + "β";
            });


            var global = {};
            global["GlobalOptionId"] = $("#GlobalOptionId").val();
            global["Name"] = $("#Name").val();
            global["DisplayName"] = $("#DisplayName").val();
            global["picklistvalue"] = picklistvalue;

            $.ajax({
                url: "/Service/Entity/AddGlobalStringMap.ashx",
                async: false,
                type: "post",
                dataType: "JSON",
                data: { "global": JSON.stringify(global)
                },
                success: function (data, textStatus) {
                    if (data.Result == "0") {
                        if (type == 1) {
                            //window.location = "/EntityPages/EntityListPage.aspx";
                            if ($("#edit").val() == "2") {
                                window.returnValue = data.Remark;
                                window.close();
                            }
                            else {
                                window.location = "GlobalStringMapListPage.aspx";
                            }
                        }
                        else if (type == 2) {
                            location.reload();
                        }
                    }
                    else {
                        alert(data.Remark);
                    }

                },
                error: function (data, textStatus) {
                    alert("错误");

                }

            });
        }
        function btn_return() {
            //window.location = "/EntityPages/EntityListPage.aspx";
            if ($("#edit").val() == "2") {
                window.returnValue = "";
                window.close();
            }
            else {
                window.location = "GlobalStringMapListPage.aspx";
            }
        }

        function btnAddRow() {

            var rownum = $("#picklist_table tr").length - 1;

            var value = "<input type='text' id='value_" + rownum + "' name='value_" + rownum + "' width='90%'/>";
            var key = "<input type='text' id='key_" + rownum + "' name='key_" + rownum + "' width='90%'/>";
            var order = "<input type='text' id='order_" + rownum + "' name='order_" + rownum + "' width='50px'/>";
           
            var button = "<button type='button' class='btn btn-primary ng-binding' onclick='DelRow(" + rownum + ");'>删除</button>";
            var row = "<tr id='" + rownum + "' ><td>" + value + "</td><td>" + key + "</td><td>" + order + "</td><td>" + button + "</td></tr>";
            $(row).insertAfter($("#picklist_table tr:eq(" + rownum + ")"));
        }

        function DelRow(rownum) {

            $("#picklist_table tr[id=" + rownum + "]").remove();
        }

        $(document).ready(function () {




            if ($("#edit").val() == "0" || $("#edit").val() == "2") {
                //新建
                $("#page_title").html("新建通用选项集");
                $("[noModify = noModify]").removeAttr("disabled");
                $("#add_btn_div").show();
                $("#update_btn_div").hide();
                $("#save_btn").hide();
            }
            else if ($("#edit").val() == "1") {

                //修改
                getGlobalStringMapInfo();
                $("#page_title").html("修改通用选项集");
                $("[noModify = noModify]").attr('disabled', 'true');
                $("#add_btn_div").hide();
                $("#update_btn_div").show();
            }
            else {
                alert("edit状态错误");
                window.location = "GlobalStringMapListPage.aspx";
            }


        });


       
        function getGlobalStringMapInfo() {
            $.ajax({
                url: "/Service/Entity/GetGlobalPicklist.ashx",
                async: false,
                type: "post",
                dataType: "json",
                data: {"GlobalOptionId": $("#GlobalOptionId").val()
            },
            success: function (data, textStatus) {
                $("#DisplayName").val(data.DisplayName);
                $("#Name").val(data.Name.substring(data.Name.indexOf('_') + 1, data.Name.length));

                var arr1 = data.picklistvalue.split('β');
                for (var i = 0; i < arr1.length - 1; i++) {
                    var arr2 = arr1[i].split('α');
                    var rownum = $("#picklist_table tr").length - 1;

                    var value = "<input type='text' id='value_" + rownum + "' value='" + arr2[1] + "' name='value_" + rownum + "' width='90%'/>";
                    var key = "<input type='text' id='key_" + rownum + "' value='" + arr2[2] + "' name='key_" + rownum + "' width='90%'/>";
                    var order = "<input type='text' id='order_" + rownum + "' value='" + arr2[3] + "' name='order_" + rownum + "' width='50px'/>";
                           
                    var button = "<button type='button' class='btn btn-primary ng-binding' onclick='DelRow(" + rownum + ");'>删除</button>";
                    var row = "<tr id='" + rownum + "' picklistid='" + arr2[0] + "' ><td>" + value + "</td><td>" + key + "</td><td>" + order + "</td><td>" + button + "</td></tr>";
                    $(row).insertAfter($("#picklist_table tr:eq(" + rownum + ")"));
                }
            },
            error: function (data, textStatus) {
                alert("ajax错误");

            }

        });

    }

        function updateGlobalStringMap(type) {
            var picklistvalue = "";
            $("#picklist_table tr[id]").each(function () {
                var picklistid = $(this).attr("picklistid");
                var value = $(this).find("td").eq(0).children().val();
                var key = $(this).find("td").eq(1).children().val();
                var order = $(this).find("td").eq(2).children().val();
                picklistvalue += picklistid + "α" + value + "α" + key  + "α" + order+"β";
            });


            var global = {};
            global["GlobalOptionId"] = $("#GlobalOptionId").val();
            global["Name"] = $("#Name").val();
            global["DisplayName"] = $("#DisplayName").val();
            global["picklistvalue"] = picklistvalue;

            $.ajax({
                url: "/Service/Entity/AddGlobalStringMap.ashx",
                async: false,
                type: "post",
                dataType: "JSON",
                data: { "global": JSON.stringify(global)
                },
                success: function (data, textStatus) {
                    if (data.Result == "0") {
                        if (type == 1) {
                            //window.location = "/EntityPages/EntityListPage.aspx";
                            if ($("#edit").val() == "2") {
                                window.returnValue = data.Remark;
                                window.close();
                            }
                            else {
                                window.location = "GlobalStringMapListPage.aspx";
                            }
                        }
                        else if (type == 2) {
                            location.reload();
                        }
                    }
                    else {
                        alert(data.Remark);
                    }

                },
                error: function (data, textStatus) {
                    alert("错误");

                }

            });
        }
    </script>
    <style>
        .page-header{margin:15px;}
        .page-header+.btn-primary{margin:0px 15px 10px 5px;}
        .panel-heading{padding:3px 15px;height:40px;line-height:34px;}
    </style>
</head>
<body>
    <form id="form1" runat="server" >
    <input id="GlobalOptionId"  type="hidden" value="<%=GlobalOptionId %>" />
    <input id="edit" type="hidden" value="<%= edit %>" />
    <div class="">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header" id="page_title">
                    新建通用选项集</h1>
                       <button type='button' style="float: right" id='Button1' class='btn btn-primary ng-binding'
                            onclick="btn_return();">
                            取消</button>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <!-- /.panel-heading -->
                    <div class="panel-body">
                        <div class="form-group">
                            <label for="DisplayName">
                                通用选项集中文名</label>
                            <input type="text" class="form-control" id="DisplayName" />
                        </div>
                        <div class="form-group">
                            <label for="Name">
                                通用选项集英文名</label>
                            <div class="input-group">
                                <div class="input-group-addon">
                                    <%=Prefix%>_</div>
                                <input type="text" class="form-control" id="Name"  noModify="noModify"/>
                               
                            </div>
                        </div>
                        <div class="form-group" id="newPicklist_div" >
                            <button type="button" class="btn btn-primary ng-binding" onclick="btnAddRow();">
                                添加</button>
                            <table class="table table-condensed" id="picklist_table">
                                <thead>
                                    <tr>
                                        <th>
                                            值
                                        </th>
                                        <th>
                                            标签
                                        </th>
                                        <th>
                                           排序
                                        </th>
                                        <th>
                                            操作
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                        <div class="form-group" id="add_btn_div" style="display: none;">
                            <div class="col-sm-offset-2 col-sm-10">
                            
                                <button type="button" class="btn btn-primary ng-binding" onclick="addGlobalStringMap(1);">
                                    保存并关闭</button>
                                     <button type="button" id="save_btn" class="btn btn-primary ng-binding" onclick="addGlobalStringMap(2);">
                                    保存</button>
                                 
                            </div>
                        </div>
                         <div class="form-group" id="update_btn_div" style="display: none;">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="button" class="btn btn-primary ng-binding" onclick="updateGlobalStringMap(1);">
                                    保存并关闭</button>
                                <button type="button" class="btn btn-primary ng-binding" onclick="updateGlobalStringMap(2);">
                                    保存</button>
                            </div>
                        </div>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
        </div>
    </div>
    </form>
</body>
</html>
